using System.Collections.Generic;
using System.Dynamic;
using System.Text;
using System.Utility.System;

namespace System
{
	[Serializable]
	public class DynamicX : DynamicObject, IDynamicEnable
	{
		public Dictionary<string, object> Members;

		private DynamicX _Dynamic;

		public DynamicX Dynamic
		{
			get
			{
				return _Dynamic;
			}
			set
			{
				_Dynamic = value;
			}
		}

		public DynamicX()
		{
			Members = new Dictionary<string, object>();
		}

		public object Get(string propertyName)
		{
			object obj;
			TryGetMember(new GetPropertyBinder(propertyName), out obj);
			return obj;
		}

		public override IEnumerable<string> GetDynamicMemberNames()
		{
			return Members.Keys;
		}

		public void Set(string propertyName, object value)
		{
			TrySetMember(new SetPropertyBinder(propertyName), value);
		}

		public string ToString(Dictionary<string, string> codenames)
		{
			string empty;
			if (!TypeExtension.IsInvalid(Members))
			{
				StringBuilder stringBuilder = new StringBuilder();
				TypeExtension.ForEach(Members, (KeyValuePair<string, object> s) => {
				    if (s.Key != "XLYLogString")
				    {
				        string str = (codenames.TryGetValue(s.Key, out str) ? str : s.Key);
				        stringBuilder.Append(str).Append(" : ").Append(TypeExtension.ToSafeString(s.Value));
				        stringBuilder.Append("\t");
				    }
				});
				empty = TypeExtension.TrimEnd(stringBuilder.ToString(), Environment.NewLine, StringComparison.Ordinal);
			}
			else
			{
				empty = string.Empty;
			}
			return empty;
		}

		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			Members.TryGetValue(binder.Name, out result);
			return true;
		}

		public override bool TrySetMember(SetMemberBinder binder, object value)
		{
			Members[binder.Name] = value;
			return true;
		}
	}
}